package com.example.activetest;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

public class ConsumerMq {
    public static void main(String[] args) throws JMSException {
        //            建立mq的连接
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnectionFactory.DEFAULT_USER, ActiveMQConnectionFactory.DEFAULT_PASSWORD, "tcp://127.0.0.1:61616");
//            创立连接
        Connection createConnection = connectionFactory.createConnection();
//            启动连接
        createConnection.start();
//            创立会话工厂
        Session session = createConnection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);
//          创建队列
        Destination destination = session.createQueue("test_queue");
        MessageConsumer consumer = session.createConsumer(destination);
        while (true){
//            建立监听
            TextMessage textMessage = (TextMessage) consumer.receive();
            if (textMessage !=null){
                String s = textMessage.getText();
                System.out.println("消息为:"+s);
            }else {
                break;
            }
        }
        System.out.println("完毕");

    }
    }

